package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.resources.OrganizeJoinRes;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.ArrayList;
import java.util.List;

public abstract class JoinOrganizeCommActivity extends SlidingActivity implements View.OnClickListener, ResultSubscriber.OnResultListener {

    protected int type;
    protected String id;
    protected String depaId;
    protected String classId;
    protected List<int[]> itemTitles;
    protected List<TextView> itemDetails;
    protected Intent intent;
    private boolean fromPre;

    @Override
    protected int getResourceId() {
        return R.layout.activity_comm_organize_join;
    }

    @Override
    protected void init() {
        this.id = "";
        this.depaId = "";
        this.classId = "";

        this.intent = getIntent();
        this.type = getIntent().getIntExtra(OrganizeJoinRes.ORGANIZED_TYPE, 0);
        this.itemTitles = new ArrayList<>();
        this.itemDetails = new ArrayList<>();

        this.itemTitles.add(OrganizeJoinRes.COMPANY_ITEM_TITLES);
        this.itemTitles.add(OrganizeJoinRes.SCHOOL_ITEM_TITLES);
        this.itemTitles.add(OrganizeJoinRes.ZHONG_CHUANG_ZONE_ITEM_TITLES);
        this.itemTitles.add(OrganizeJoinRes.OTHERS_ORGANIZE_ITEM_TITLES);

        initTitle(OrganizeJoinRes.MAIN_TITLES[this.type]);
        initItems();

        this.findViewById(R.id.icon_left).setOnClickListener(this);
        this.findViewById(R.id.join_others_organize_btn_request).setOnClickListener(this);
    }

    private void initTitle(int title) {
        topName.setText(title);
        topDesc.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        rightIcon.setVisibility(View.GONE);
    }

    private void initItems(){
        LinearLayout linearLayout = (LinearLayout) this.findViewById(R.id.organize_msg_lly);
        for(int i = 0; i < itemTitles.get(this.type).length; i++){
            View item = View.inflate(this, R.layout.item_organize_choose, null);
            ((TextView)item.findViewById(R.id.join_organize_tv_title)).setText(itemTitles.get(this.type)[i]);
            item.setTag(i);
            item.setOnClickListener(this);
            linearLayout.addView(item);
            this.itemDetails.add((TextView) item.findViewById(R.id.join_organize_tv_detail));
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.join_others_organize_btn_request){ // 申请加入
            for(int i = 0; i < this.itemDetails.size(); i++){
                if(this.itemDetails.get(i).getText().toString().trim().length() == 0 && i < 3) {
                    ToasUtil.toast(this, R.string.toast_organiza_commit_params_remind);
                    return;
                }
            }
//            showLoading();
            loading.show();
            JsonObject param = new JsonObject();
            param.addProperty("token", HttpHelper.token);
            param.addProperty("id", this.id);
            param.addProperty("type", this.type+1);
            param.addProperty("depaId", this.depaId);
            if(this.classId != null && this.classId.length() > 0)
                param.addProperty("classId", this.classId);
            String position = this.itemDetails.get(OrganizeJoinRes.SCHOOL_ITEM_TITLES.length-2).getText().toString();
            if(position != null && position.length() > 0)
                param.addProperty("position", position);
            if(this.type == OrganizeJoinRes.SCHOOL_INDEX) {
                String joinSchoolTime = this.itemDetails.get(OrganizeJoinRes.SCHOOL_ITEM_TITLES.length - 1).getText().toString().replace("年", "");
                if (joinSchoolTime != null && joinSchoolTime.length() > 0)
                    param.addProperty("startTime", joinSchoolTime);
            }
            HttpHelper.getInstance(this).commitJoinOrganize(param, this.type, this);
        } else if(v.getId() == R.id.icon_left){ // 返回
            this.finish();
        } else {    // 选择条目
            addAllIdBeforItemClick((Integer) v.getTag());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || null == data || requestCode >= this.itemDetails.size() || requestCode < 0) {
            if(fromPre){
                switch (requestCode){
                    case 1:
                        this.itemDetails.get(1).setText("");
                    case 2:
                        this.itemDetails.get(2).setText("");
                    default:
                        fromPre = false;
                }
            }
            return;
        }
        fromPre = false;
        String result = data.getStringExtra(OrganizeJoinRes.RESPONSE_RESULT);
        this.itemDetails.get(requestCode).setText(result);
        this.id = data.getStringExtra(OrganizeJoinRes.ID);
        this.depaId = data.getStringExtra(OrganizeJoinRes.DEPA_ID);
        this.classId = data.getStringExtra(OrganizeJoinRes.CLASS_ID);

        if (requestCode < this.itemDetails.size() - 2
        || this.itemDetails.get(this.itemDetails.size() - 1).getText().toString().trim().length() == 0) {
            fromPre = true;
            addAllIdBeforItemClick(requestCode + 1);
        }
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {}
    @Override
    public void onError(int requestType) {
        loading.cancel();
        ToasUtil.toast(this, R.string.toast_organiza_commit_error);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(requestType != this.type)
            return;
        if(data.getErrcode() == 0){
            ToasUtil.toast(this, R.string.toast_organiza_commit_success);

            setResult(RESULT_OK, new Intent(this, DepartmentCreateActivity.class)
                    .putExtra(OrganizeJoinRes.ORGANIZED_NAME, itemDetails.get(0).getText().toString()));
//            cancleLoading();
            loading.cancel();
//            startActivity(new Intent(this, OrganizeActivity.class));
            finish();
        } else {
            ToasUtil.toast(this, R.string.toast_organiza_commit_fail);
        }
    }

    private void addAllIdBeforItemClick(int index){
        intent.putExtra(OrganizeJoinRes.ID, id);
        intent.putExtra(OrganizeJoinRes.DEPA_ID, depaId);
        intent.putExtra(OrganizeJoinRes.CLASS_ID, classId);
        itemClick(index);
    }
    abstract protected void itemClick(int index);
}
